package com.sanjiahantang.youjiarole.module.me.order.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.module.me.order.beans.ServiceListBean;

import java.util.List;

/**
 * Created by zy on 2017/9/27.
 */

public class ServiceOrderAdapter extends RecyclerView.Adapter {

    private Context context;
    private LayoutInflater inflater;
    //private ServiceListBean  data;

    private List<ServiceListBean.Data> data;
    public ServiceOrderAdapter(Context context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
    }

    public List<ServiceListBean.Data> getData() {
        return data;
    }

    public void setData(List<ServiceListBean.Data> data) {
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        return new OrderHolder(inflater.inflate(R.layout.item_serviceorder,null), data.get(viewType),viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((OrderHolder)holder).bindData(data.get(position),position);
    }
    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return data==null ?0:data.size();
    }
private  Button btn_besure;
    private class OrderHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        public OrderHolder(View itemView,ServiceListBean.Data  data, int position) {
            super(itemView);
            bindData(data,position);
        }

        private void bindData(ServiceListBean.Data  data, int position) {
                bindGoodsList(itemView,data.getServiceRecordList());
        }
        private void bindGoodsList(View itemView, List<ServiceListBean.Data.ServiceRecordListBean> goods) {
            TextView txtOrderno= (TextView) itemView.findViewById(R.id.service_orderNO);
            TextView txtOrderStatus= (TextView) itemView.findViewById(R.id.service_orderStatus);
            ListView ServiceOrderListView = (ListView) itemView.findViewById(R.id.list_service_order);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.service_count);
            TextView txtMoney = (TextView) itemView.findViewById(R.id.service_money);
            Button btn_cancle= (Button) itemView.findViewById(R.id.cancle_service_order);
            btn_besure= (Button) itemView.findViewById(R.id.besure_service_order);
            btn_cancle.setOnClickListener(this);
            btn_besure.setOnClickListener(this);
            String orderno = null;
            String statusname=null;

            int count = 0;//数量
            float price = 0f;
            for(ServiceListBean.Data.ServiceRecordListBean item : goods){
                count += item.getPayCount();
                price += Float.parseFloat(String.valueOf(item.getResult()));
                orderno=item.getOrderRecordCode();
                statusname= item.getStatusName();
                switch (item.getStatusId()){
                    case 0://dai
                        btn_cancle.setText("取消付款");
                        btn_besure.setText("付款");
                        break;
                    case 1://bufen
                        btn_cancle.setVisibility(View.GONE);
                        btn_besure.setText("付款");
                        break;
                    case 2://yifu
                        btn_cancle.setVisibility(View.GONE);
                        btn_besure.setText("付款");
                        break;
                    case 3://yiwancheng
                        btn_cancle.setVisibility(View.GONE);
                        btn_besure.setText("评价");
                        break;
                }
            }
            txtMoney.setText(String.format("%.2f",price));
            txtGoodsCount.setText(String.valueOf(goods.size()));
            txtOrderStatus.setText(statusname);
            txtOrderno.setText(orderno);
            //txtOrderStatus.setText(goods.get(0).getStatusId());
            ServiceOrderListView.setAdapter(new QuickAdapter<ServiceListBean.Data.ServiceRecordListBean>(context,R.layout.item_serviceorderitem,goods) {
                @Override
                protected void convert(BaseAdapterHelper helper, ServiceListBean.Data.ServiceRecordListBean item) {
                    helper.setText(R.id.sg_name,item.getServiceName());
                    helper.setText(R.id.sg_price,String.valueOf(item.getPrice()));
                    helper.setText(R.id.sg_count,String.valueOf(item.getCount()));
                }
            });
        }

        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.cancle_service_order:
                    Utils.showToast(context,"取消支付按钮");
                    break;
                case R.id.besure_service_order:
                    if (btn_besure.getText().toString().equals("评价")){
                        Utils.showToast(context,"跳转评价页面支付按钮");
                    }else {
                        Utils.showToast(context,"跳转到支付页面");
                    }

                    break;
            }

        }
    }
}
